﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HomeWork08;

namespace OrderForms
{

    public partial class Form1 : Form
    {
        OrderService myOrder = new OrderService();
        public string OrderNum { get; set; }
        public Form1()
        {
            InitializeComponent();
            Form1_Load();
        }

        private void Form1_Load()
        {
            try
            {
                myOrder.CreatOrder("20200320", "地址1", "张三");
                myOrder.addOrder("20200320", "手机壳", 20, 1);
                myOrder.addOrder("20200320", "充电宝", 100, 1);
                myOrder.addOrder("20200320", "充电线", 30, 2);
                myOrder.CreatOrder("20200321", "地址2", "客户2");
                myOrder.addOrder("20200321", "乒乓球拍", 55, 1);
                myOrder.CreatOrder("20200000", "地址3", "客户3");
                myOrder.addOrder("20200000", "笔记本", 5000, 1);

            }
            catch (MyException e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void btnCreatOrder_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(myOrder, "");
            form2.Text = "创建订单";
            form2.ShowDialog();
            ordersBindingSource.ResetBindings(false);

        }

        private void btnDelet_Click(object sender, EventArgs e)
        {
            string num = "";
            if(myOrder.orders.Count!=0)
                 num = (string)orderDataGridView.Rows[orderDataGridView.
                                    CurrentRow.Index].Cells[0].Value;
            myOrder.DelOrder(num);
            ordersBindingSource.ResetBindings(false);
        }

        private void btnAddOrder_Click(object sender, EventArgs e)
        {
            string orderNum= (string)orderDataGridView.Rows[orderDataGridView.
                                    CurrentRow.Index].Cells[0].Value;
            Form2 form2 = new Form2(myOrder,orderNum);
            form2.Text = "添加订单明细";
            form2.ShowDialog();
            ordersBindingSource.ResetBindings(false);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            ordersBindingSource.DataSource = myOrder.FindOrder(OrderNum);
            ordersBindingSource.ResetBindings(false);
            if (OrderNum.Equals(string.Empty)) 
            {
                ordersBindingSource.DataSource = myOrder.orders;
                ordersBindingSource.ResetBindings(false);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            findTextBox.DataBindings.Add("Text", this, "OrderNum");
            ordersBindingSource.DataSource = myOrder.orders;
        }

        private void btnOut_Click(object sender, EventArgs e)
        {
            myOrder.Export();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            ordersBindingSource.DataSource = myOrder.Import();
            ordersBindingSource.ResetBindings(false);

        }
    }
}
